﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace jnxc.Web.plantharmfulinfo
{
    /// <summary>
    /// GetPH 的摘要说明
    /// </summary>
    public class GetPH : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.plantharmfulinfo bll = new jnxc.BLL.plantharmfulinfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int pmid = 0;
                string uidstr = context.Request.Form["phid"];
                if (uidstr != "")
                    pmid = int.Parse(uidstr);
                string h = context.Request.Form["hid"];
                string c = context.Request.Form["pid"];
                int hid = 0, cid = 0;
                if (h != "") hid = int.Parse(h);
                if (c != "") cid = int.Parse(c);
                if (hid != 0 && cid != 0)
                {
                    jnxc.Model.plantharmfulinfo model = new jnxc.Model.plantharmfulinfo();
                    model.hid = hid;
                    model.pid = cid;
                    model.phid = pmid;
                    if (bll.Update(model))
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string[] h = context.Request.QueryString["hids"].Split(',');
                string[] c = context.Request.QueryString["pids"].Split(',');
                int hid = 0, pid = 0;
                jnxc.Model.plantharmfulinfo model = new jnxc.Model.plantharmfulinfo();
                int i = 0;
                foreach (string hi in h)
                {
                    foreach (string ci in c)
                    {
                        hid = int.Parse(hi);
                        pid = int.Parse(ci);
                        if (hid != 0 && pid != 0)
                        {
                            model.hid = hid;
                            model.pid = pid;
                            
                            i = i + bll.Add(model);
                        }
                    }
                }
                if (i > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";            
            string uidstr = context.Request.QueryString["phid"];            
            if (bll.DeleteList(uidstr))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();

            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }

        }
        public void Query(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
            string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            if (pro != "")
            {
                //===============================================================  
                //获取查询条件:【用户id,开始时间，结束时间，关键字】  
                string uname, utruename, st;
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }


                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());

                }
                if (null != context.Request.Form["page"])
                {

                    page = int.Parse(context.Request.Form["page"].ToString().Trim());

                }
                if (null != context.Request.Form["sort"])
                {

                    order = context.Request.Form["sort"].ToString().Trim();

                }
                if (null != context.Request.Form["order"])
                {

                    sort = context.Request.Form["order"].ToString().Trim();

                }


                //===================================================================  
                //组合查询语句：条件+排序  
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" pname like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" hname like '%{0}%' and ", utruename);
                }
                strWhere.AppendFormat(" province='{0}' and ", pro);

                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    //strWhere.AppendFormat(" order by {0} {1}", sort, order);//添加排序  
                    oderby = order + " " + sort;
                }
                //DataSet ds = Bnotice.GetList(strWhere.ToString());  //调用不分页的getlist  

                //调用分页的GetList方法  
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count, 50);//DataSet数据转化为Json数据                
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }

        }  
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}